package www13;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;

// 球队类，存球队各种数据
class FootballTeam {
    private String teamName;    // 球队名字
    private int numPlayedGames;         // 已比赛场次
    private int numWonGames;         // 胜场
    private int numDrawnGames;         // 平场
    private int numLostGames;         // 负场
    private int numGoalsScored;         // 胜球
    private int numGoalsConceded;         // 失球
    private int goalDifference;         // 净胜球
    private int teamPoints;        // 球队积分

    // 初始化球队，名字给定，其他数据设为 0
    public FootballTeam(String teamName) {
        this.teamName = teamName;
        this.numPlayedGames = 0;
        this.numWonGames = 0;
        this.numDrawnGames = 0;
        this.numLostGames = 0;
        this.numGoalsScored = 0;
        this.numGoalsConceded = 0;
        this.goalDifference = 0;
        this.teamPoints = 0;
    }

    // 获取球队名字的方法
    public String getTeamName() {
        return teamName;
    }

    // 获取比赛场次的方法
    public int getNumPlayedGames() {
        return numPlayedGames;
    }

    // 设置比赛场次的方法
    public void setNumPlayedGames(int numPlayedGames) {
        this.numPlayedGames = numPlayedGames;
    }

    // 获取胜场数的方法
    public int getNumWonGames() {
        return numWonGames;
    }

    // 设置胜场数的方法
    public void setNumWonGames(int numWonGames) {
        this.numWonGames = numWonGames;
    }

    // 获取平场数的方法
    public int getNumDrawnGames() {
        return numDrawnGames;
    }

    // 设置平场数的方法
    public void setNumDrawnGames(int numDrawnGames) {
        this.numDrawnGames = numDrawnGames;
    }

    // 获取负场数的方法
    public int getNumLostGames() {
        return numLostGames;
    }

    // 设置负场数的方法
    public void setNumLostGames(int numLostGames) {
        this.numLostGames = numLostGames;
    }

    // 获取进球数的方法
    public int getNumGoalsScored() {
        return numGoalsScored;
    }

    // 设置进球数的方法
    public void setNumGoalsScored(int numGoalsScored) {
        this.numGoalsScored = numGoalsScored;
    }

    // 获取失球数的方法
    public int getNumGoalsConceded() {
        return numGoalsConceded;
    }

    // 设置失球数的方法
    public void setNumGoalsConceded(int numGoalsConceded) {
        this.numGoalsConceded = numGoalsConceded;
    }

    // 获取球队积分的方法
    public int getTeamPoints() {
        return teamPoints;
    }

    // 设置球队积分的方法
    public void setTeamPoints(int teamPoints) {
        this.teamPoints = teamPoints;
    }

    // 获取净胜球数的方法
    public int getGoalDifference() {
        return goalDifference;
    }

    // 设置净胜球数的方法
    public void setGoalDifference(int goalDifference) {
        this.goalDifference = goalDifference;
    }

    // 根据比赛进球更新球队数据
    // 看主客队进球，更新比赛场次、进球、失球、净胜球、积分等数据
    public void updateTeamStats(int homeTeamGoals, int awayTeamGoals) {
        numPlayedGames++;          // 比赛一场，场次加 1
        numGoalsScored += homeTeamGoals;    // 加上主场进球数
        numGoalsConceded += awayTeamGoals;    // 加上客场进球数（主场失球数）
        goalDifference = numGoalsScored - numGoalsConceded;        // 算净胜球

        if (homeTeamGoals > awayTeamGoals) {  // 主场赢了
            numWonGames++;
            teamPoints += 3;    // 赢了得 3 分
        } else if (homeTeamGoals < awayTeamGoals) {  // 主场输了
            numLostGames++;
        } else {  // 平局
            numDrawnGames++;
            teamPoints += 1;    // 平了得 1 分
        }
    }
}

// 足球联赛积分管理主类
public class FootballLeaguePointsManagement {
    public static void main(String[] args) throws IOException {
        File file = new File("甲A联赛积分表.txt");  // 积分表文件，文件名修改为甲A联赛相关
        List<FootballTeam> footballTeams = new ArrayList<>();  // 存球队对象的列表
        Scanner scanner = new Scanner(System.in);  // 用于读取控制台输入

        // 初始化甲A联赛的部分球队并添加到列表
        FootballTeam team1 = new FootballTeam("云南玉昆");
        FootballTeam team2 = new FootballTeam("大连英博");
        FootballTeam team3 = new FootballTeam("广州俱乐");
        FootballTeam team4 = new FootballTeam("重庆铜梁");
        FootballTeam team5 = new FootballTeam("苏州东吴");
        FootballTeam team6 = new FootballTeam("辽宁铁人");
        FootballTeam team7 = new FootballTeam("广西平果");
        FootballTeam team8 = new FootballTeam("石家庄功");
        footballTeams.add(team1);
        footballTeams.add(team2);
        footballTeams.add(team3);
        footballTeams.add(team4);
        footballTeams.add(team5);
        footballTeams.add(team6);
        footballTeams.add(team7);
        footballTeams.add(team8);


        // 处理文件，没有就创建，有就读取数据并展示
        if (!file.exists()) {
            file.createNewFile();  // 创建新文件
            System.out.println("没找到甲A联赛积分表，正在创建...");
        } else {
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                String line;
                while ((line = br.readLine())!= null) {
                    String[] parts = line.split(" ");

                    // 从文件数据创建球队对象并设置属性
                    String teamName = parts[0];
                    int numPlayedGames = Integer.parseInt(parts[1]);
                    int numWonGames = Integer.parseInt(parts[2]);
                    int numDrawnGames = Integer.parseInt(parts[3]);
                    int numLostGames = Integer.parseInt(parts[4]);
                    int numGoalsScored = Integer.parseInt(parts[5]);
                    int numGoalsConceded = Integer.parseInt(parts[6]);
                    int goalDifference = Integer.parseInt(parts[7]);
                    int teamPoints = Integer.parseInt(parts[8]);

                    FootballTeam footballTeam = null;
                    for (FootballTeam ft : footballTeams) {
                        if (ft.getTeamName().equals(teamName)) {
                            footballTeam = ft;
                            break;
                        }
                    }

                    if (footballTeam!= null) {
                        footballTeam.setNumPlayedGames(numPlayedGames);
                        footballTeam.setNumWonGames(numWonGames);
                        footballTeam.setNumDrawnGames(numDrawnGames);
                        footballTeam.setNumLostGames(numLostGames);
                        footballTeam.setNumGoalsScored(numGoalsScored);
                        footballTeam.setNumGoalsConceded(numGoalsConceded);
                        footballTeam.setGoalDifference(goalDifference);
                        footballTeam.setTeamPoints(teamPoints);
                    }
                }
            }
            // 展示读取到的积分表数据
            System.out.println("原有甲A联赛积分表：");
            System.out.println("队名\t场次\t胜场\t平场\t负场\t进球\t失球\t净胜球\t积分");
            for (FootballTeam footballTeam : footballTeams) {
                System.out.println(footballTeam.getTeamName() + "\t" + footballTeam.getNumPlayedGames() + "\t" + footballTeam.getNumWonGames() + "\t" + footballTeam.getNumDrawnGames() + "\t" + footballTeam.getNumLostGames() + "\t" + footballTeam.getNumGoalsScored() + "\t" + footballTeam.getNumGoalsConceded() + "\t" + footballTeam.getGoalDifference() + "\t" + footballTeam.getTeamPoints());
            }
        }

        while (true) {
            System.out.println();
            System.out.println("输入比赛结果 (主队 客队 主队得分 客队得分):");
            String input = scanner.nextLine();
            if ("exit".equals(input)) {
                break;
            }
            String[] matchParts = input.split(" ");
            if (matchParts.length!= 4) {
                System.out.println("输入错了，重新输");
                continue;
            }
            String homeTeamName = matchParts[0];
            String awayTeamName = matchParts[1];
            int homeTeamGoals;
            int awayTeamGoals;
            try {
                homeTeamGoals = Integer.parseInt(matchParts[2]);
                awayTeamGoals = Integer.parseInt(matchParts[3]);
            } catch (NumberFormatException e) {
                System.out.println("输入错了，重新输");
                continue;
            }

            FootballTeam homeTeam = null;
            FootballTeam awayTeam = null;

            // 找或创建主队对象
            for (FootballTeam footballTeam : footballTeams) {
                if (footballTeam.getTeamName().equals(homeTeamName)) {
                    homeTeam = footballTeam;
                    break;
                }
            }
            if (homeTeam == null) {
                homeTeam = new FootballTeam(homeTeamName);
                footballTeams.add(homeTeam);
            }

            // 找或创建客队对象
            for (FootballTeam footballTeam : footballTeams) {
                if (footballTeam.getTeamName().equals(awayTeamName)) {
                    awayTeam = footballTeam;
                    break;
                }
            }
            if (awayTeam == null) {
                awayTeam = new FootballTeam(awayTeamName);
                footballTeams.add(awayTeam);
            }

            homeTeam.updateTeamStats(homeTeamGoals, awayTeamGoals);
            awayTeam.updateTeamStats(awayTeamGoals, homeTeamGoals);

            // 按积分、净胜球、进球数排序球队列表并展示积分表
            Collections.sort(footballTeams, new Comparator<FootballTeam>() {
                @Override
                public int compare(FootballTeam t1, FootballTeam t2) {
                    if (t1.getTeamPoints()!= t2.getTeamPoints()) {
                        return Integer.compare(t2.getTeamPoints(), t1.getTeamPoints());
                    } else if (t1.getGoalDifference()!= t2.getGoalDifference()) {
                        return Integer.compare(t2.getGoalDifference(), t1.getGoalDifference());
                    } else {
                        return Integer.compare(t2.getNumGoalsScored(), t1.getNumGoalsScored());
                    }
                }
            });

            System.out.println();
            System.out.println("甲A联赛积分表：");
            System.out.println("队名\t场次\t胜场\t平场\t负场\t进球\t失球\t净胜球\t积分");
            for (FootballTeam footballTeam : footballTeams) {
                System.out.println(footballTeam.getTeamName() + "\t" + footballTeam.getNumPlayedGames() + "\t" + footballTeam.getNumWonGames() + "\t" + footballTeam.getNumDrawnGames() + "\t" + footballTeam.getNumLostGames() + "\t" + footballTeam.getNumGoalsScored() + "\t" + footballTeam.getNumGoalsConceded() + "\t" + footballTeam.getGoalDifference() + "\t" + footballTeam.getTeamPoints());
            }

            // 把更新后的球队数据写入文件
            try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) {
                for (FootballTeam footballTeam : footballTeams) {
                    bw.write(footballTeam.getTeamName() + " " + footballTeam.getNumPlayedGames() + " " + footballTeam.getNumWonGames() + " " + footballTeam.getNumDrawnGames() + " " + footballTeam.getNumLostGames() + " " + footballTeam.getNumGoalsScored() + " " + footballTeam.getNumGoalsConceded() + " " + footballTeam.getGoalDifference() + " " + footballTeam.getTeamPoints());
                    bw.newLine();
                }
            }
        }
    }
}